package condominios.servlets.residente;

import java.io.IOException;
import java.util.Collection;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import condominios.excepcion.DAOExcepcion;
import condominios.modelo.Persona;
import condominios.negocio.GestionPersonas;
/**
 * Servlet implementation class ResidenteBuscarServlet
 */
@WebServlet("/ResidenteBuscarServlet")
public class ResidenteBuscarServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	int numInstancia = 0;
	int numFilas;
	int minRow = 0;
	int maxRow = 4;
	int numPag = 1;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ResidenteBuscarServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		HttpSession session = request.getSession();
		Persona vo = (Persona)session.getAttribute("user");
		session.setAttribute("CANTIDAD_FILAS",5);
				
		if (vo== null){
			RequestDispatcher rd = request.getRequestDispatcher("error.jsp");
			rd.forward(request, response);
			return;
		}
		System.out.println(numInstancia); 
		System.out.println(numFilas);
		System.out.println("Dentro de doPost del servlet ResidenteBuscarSerlvet");
		if(request.getParameter("txtNombres")!=null) {
			
			numFilas = 5;
			//if (numInstancia > 1) {
				//numFilas = (int) session.getAttribute("CANTIDAD_FILAS");
			//}
			
			int numPaginas;
			numInstancia += 1;		
			String x = request.getParameter("txtNombres");
					
			minRow = (numPag*numFilas)-numFilas;
			maxRow = minRow + (numFilas-1);
			
			System.out.println(minRow);
			System.out.println(numFilas);
			
			GestionPersonas persona = new GestionPersonas();
			try {
				Collection<Persona> lista = persona.buscarPorNombreDNI(x);
				
				session.setAttribute("LISTAPERSONAS", lista);
				session.setAttribute("NUMEROPAGINA","1");
				session.setAttribute("NUMEROREGISTROS", lista.size());
				if (maxRow > (lista.size()-1)){
					
					maxRow = lista.size()-1; 
				}
				if (lista.size() % numFilas == 0){
					numPaginas = ((int) Math.floor(lista.size()/numFilas));
				}
				else {
					numPaginas = ((int) Math.floor(lista.size()/numFilas)) +1;
				}
					
				// Guardar en el ambiente de request
				
				request.setAttribute("RESIDENTES", lista);
				request.setAttribute("NUMEROPAGINAS",numPaginas);
				request.setAttribute("MINROW", minRow);
				request.setAttribute("MAXROW", maxRow);
				request.setAttribute("NUMFILAS", numFilas);
				request.setAttribute("NUMPAG", 1);
				RequestDispatcher rd = request.getRequestDispatcher("residente_buscar.jsp");
				rd.forward(request, response);

			} catch (DAOExcepcion e) {
				System.out.println(e.getMessage());
				RequestDispatcher rd = request.getRequestDispatcher("error.jsp");
				rd.forward(request, response);
			}
			
		}
		
	}

}
